package io.renren.modules.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.ConvertUtils;
import io.renren.common.utils.TreeUtils;
import io.renren.modules.health.dao.PostsCommentsDao;
import io.renren.modules.health.dto.PostsCommentsDTO;
import io.renren.modules.health.dto.PostsDTO;
import io.renren.modules.health.entity.PostsCommentsEntity;
import io.renren.modules.health.service.PostsCommentsService;
import io.renren.modules.security.user.SecurityUser;
import io.renren.modules.sys.dto.SysUserDTO;
import io.renren.modules.sys.service.SysRoleService;
import io.renren.modules.sys.service.SysRoleUserService;
import io.renren.modules.sys.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 帖子评论表
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2024-05-09
 */
@Service
public class PostsCommentsServiceImpl extends CrudServiceImpl<PostsCommentsDao, PostsCommentsEntity, PostsCommentsDTO> implements PostsCommentsService {

    @Autowired
    private SysUserService sysUserService;

    @Override
    public QueryWrapper<PostsCommentsEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");
        Object creator = params.get("creator");

        QueryWrapper<PostsCommentsEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);
        if (creator != null) {
            wrapper.eq("creator", Long.valueOf(creator.toString()));
        }

        return wrapper;
    }


    @Override
    public List<PostsCommentsDTO> getCommentsByPostsId(Long postsId) {
        QueryWrapper<PostsCommentsEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("posts_id", postsId);
        wrapper.orderByDesc("create_date");
        List<PostsCommentsEntity> entityList = this.baseDao.selectList(wrapper);
        List<PostsCommentsDTO> dtoList = ConvertUtils.sourceToTarget(entityList, PostsCommentsDTO.class);
        dtoList.forEach(e -> {
            SysUserDTO creatorDto = sysUserService.get(e.getCreator());
            e.setCreatorName(creatorDto == null ? "" : creatorDto.getUsername());
            SysUserDTO updaterDto = sysUserService.get(e.getUpdater());
            e.setUpdaterName(creatorDto == null ? "" : updaterDto.getUsername());
        });
        return TreeUtils.build(dtoList, Constant.MENU_ROOT);
    }

    @Override
    public PageData<PostsCommentsDTO> pagePostsComments(Map<String, Object> params) {
        // 获取当前登录用户
        Long userId = SecurityUser.getUserId();
        params.put("creator", userId);
        PageData<PostsCommentsDTO> page = this.page(params);
        page.getList().forEach(e -> {
            SysUserDTO creatorDto = sysUserService.get(e.getCreator());
            e.setCreatorName(creatorDto == null ? "" : creatorDto.getUsername());
            SysUserDTO updaterDto = sysUserService.get(e.getUpdater());
            e.setUpdaterName(creatorDto == null ? "" : updaterDto.getUsername());
        });
        return page;
    }
}